package com.joysuch.wwyt.screen.controller;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.screen.dto.ScreenMapFactoryConfigDTO;
import com.joysuch.wwyt.screen.dto.ScreenMapFactoryPointsDTO;
import com.joysuch.wwyt.screen.service.ScreenMapFactoryConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author ZhaoQiuWei
 * @Date 2023/5/6 10:32
 */
@RestController
@RequestMapping("/wwyt/screen-map-factory-config")
@Api(tags = "多分厂地图设置")
public class ScreenMapFactoryConfigController {

    @Autowired
    private ScreenMapFactoryConfigService configService;

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "保存或修改")
    @RequiresPermissions("wwyt:screen-map-factory-config:addOrUpdate")
    public ResultBean addOrUpdate(@RequestBody @Valid ScreenMapFactoryConfigDTO dto) {
        try {
            return ResultBean.success(configService.addOrUpdate(dto));
        } catch (ApiCoreException e) {
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查看")
    public ResultBean list(@RequestParam("orgCode") String orgCode, @RequestParam("tenantId") Long tenantId) {
        return ResultBean.success(configService.getList(orgCode, tenantId));
    }


    @GetMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("wwyt:screen-map-factory-config:delete")
    public ResultBean delete(@RequestParam("id") Long id) {
        configService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/getBuildingIdByLocation")
    @ApiOperation(value = "根据点位信息获取分厂buildingId")
    public ResultBean getBuildingIdByLocation(@RequestBody @Valid ScreenMapFactoryPointsDTO dto) {
        return ResultBean.success(configService.getBuildingIdByLocation(dto));
    }

    @GetMapping("/getMapPoints")
    @ApiOperation(value = "读取地图点位信息")
    public ResultBean getMapPoints(String buildingId) throws Exception {
        return ResultBean.success(configService.getMapPoints(buildingId));
    }

    @GetMapping("editOrder")
    @ApiOperation("设置顺序")
    @RequiresPermissions("wwyt:screen-map-factory-config:editOrder")
    public ResultBean editFieldOrder(Long upId, Long downId) {
        configService.editOrder(upId, downId);
        return ResultBean.success("");
    }

}
